/*******************************************************************************
 * CBNewsViewController.m
 * ComicsBlog
 *
 * Adrien Bougouin <adrien.bougouin@gmail.com>
 */

#import "CBNewsViewController.h"
#import "../CBPodcastManager.h"

@implementation CBNewsViewController

@synthesize _newsIdentifier;
@synthesize _newsView;

- (id) initWithNews:(CBNews *) news {
  self = [super init];

  if(self) {
    // add a new autorelease pool to the stack
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSURL *baseURL = [[NSURL alloc] initWithString:CBNewsBaseURL];
    NSLocale *frLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"];
    NSDateFormatter *dateInputFormat = [[NSDateFormatter alloc] init];
    [dateInputFormat setDateFormat:@"yyyy-MM-dd"];
    [dateInputFormat setLocale:frLocale];
    NSDateFormatter *dateOutputFormat = [[NSDateFormatter alloc] init];
    [dateOutputFormat setDateFormat:@"'Le' dd MMMM yyyy"];
    [dateOutputFormat setLocale:frLocale];
    NSString *title = [[news title] uppercaseString];
    NSString *newsDate = [news pubDate];
    NSDate *date = [dateInputFormat dateFromString:newsDate];
    NSString *htmlString;

    if(![[news type] isEqual:@"zik"]) {
      htmlString = [[NSString alloc] initWithFormat:@"<html>%@<body><img src=\"%@\"/><br/><div class=\"con\"><strong class=\"title\">%@</strong><br/><span class=\"date\">%@ par </span><strong class=\"author\">%@.</strong><br/>%@</div></body></html>",
                                                    CBNewsWebPageHeader,
                                                    [news hdImage],
                                                    title,
                                                    [dateOutputFormat stringFromDate:date],
                                                    [news author],
                                                    [news description]];
    } else {
      NSString *audioPlayer = [[NSString alloc] initWithFormat:@"<div><audio controls=\"controls\" src=\"%@\" onplay=\"audioCallback();\"></audio><div>",
                                                               [news file]];

      htmlString = [[NSString alloc] initWithFormat:@"<html>%@<body><div class=\"con\"><strong class=\"title\">%@</strong><br/><span class=\"date\">%@ par </span><strong class=\"author\">%@.</strong></div><br/><img src=\"%@\"/>%@<div class=\"con\">%@</div></body></html>",
                                                    CBNewsWebPageHeader,
                                                    title,
                                                    [dateOutputFormat stringFromDate:date],
                                                    [news author],
                                                    [news hdImage],
                                                    audioPlayer,
                                                    [news description]];

      [audioPlayer release];
    }

    _newsIdentifier = [news identifier];

    if([CBPodcastManager isPlayingPodcast]
       && [CBPodcastManager podcastIdentifier] == _newsIdentifier) {
      _newsView  = [[CBPodcastManager podcastNewsView] retain];

      [_newsView setDelegate:self];
    } else {
      _newsView  = [[UIWebView alloc] init];

      [_newsView setDelegate:self];
      [_newsView loadHTMLString:htmlString baseURL:baseURL];
    }

    [self setTitle:@"Details"];
    [self setView:_newsView];

    // memory management
    [baseURL          release];
    [frLocale         release];
    [dateInputFormat  release];
    [dateOutputFormat release];
    [htmlString       release];

    // delete the local autorelease pool then dealloc autoreleased objects used
    // by the method
    [pool release];
  }

  return self;
}

- (void) dealloc {
  [super dealloc];

  [_newsView stopLoading];

  [_newsView    release];
}

- (void) audioCallback {
  // store on CBPodcastManager
  [CBPodcastManager changePlayedPodcastNewsView:self];
}

//==============================================================================
// UIWebViewDelegate
//==============================================================================

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
  return YES;
}

- (BOOL)             webView:(UIWebView *) webView
  shouldStartLoadWithRequest:(NSURLRequest *) request
              navigationType:(UIWebViewNavigationType) navigationType {
  NSString *requestString = [[request URL] absoluteString];

  // intercept custom location change, URL begins with "js-call:"
  if ([requestString hasPrefix:@"js-frame:"]) {
    // extract the selector name from the URL
    NSArray *components = [requestString componentsSeparatedByString:@":"];
    NSString *function = [components objectAtIndex:1];

    // call the given selector
    [self performSelector:NSSelectorFromString(function)];

    // cancel the location change
    return NO;
  }

  // authorize first load but not other actions
  return (navigationType == UIWebViewNavigationTypeOther);
}

@end

